﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using MyDMS.DAL;

namespace MyDMS_ExtJS
{
    public partial class Default : Page
    {
        private MyDMSEntity _myDmsEntity = new MyDMSEntity();
        public static Author Author;
        protected static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string userName = tbxUserName.Text.Trim();
            string password = tbxPassword.Text.Trim();

            Office office =
                _myDmsEntity.Office.FirstOrDefault(
                    (o => o.Office_Name == userName));
            if(office!=null)
            {
                if(office.Account_Password==password)
                {
                    LoginOffice(office);

                    logger.Info(String.Format("登录成功：管理员“{0}”", office.Office_Name));
                    FormsAuthentication.RedirectFromLoginPage(userName, true);
                    return;
                }
                else
                {
                    ExtAspNet.Alert.Show("管理员账户密码错误，请核对后重新输入！");
                    return;
                }
            }

            User user = _myDmsEntity.User.FirstOrDefault(u => u.User_Name == userName);

            if (user != null)
            {
                if (user.User_Password==password)
                {
                    if (!user.Is_Active)
                    {
                        ExtAspNet.Alert.Show("用户未启用，请联系管理员！");
                    }
                    else
                    {
                        // 登录成功
                        LoginOnlineUser(user);
                        user.Updation_Date = DateTime.Now;
                        _myDmsEntity.SaveChanges();

                        logger.Info(String.Format("登录成功：用户“{0}”", user.FirstName+user.LastName));
                        FormsAuthentication.RedirectFromLoginPage(userName, true);
                        return;
                    }
                }
                else
                {
                    logger.Warn(String.Format("登录失败：用户“{0}”密码错误", userName));
                    ExtAspNet.Alert.Show("用户名或密码错误！");
                    return;
                }

            }
            else
            {
                logger.Warn(String.Format("登录失败：用户“{0}”不存在", userName));
                Alert.Show("用户名或密码错误！");
                return;
            }
        }


        #region Authenticate Related

        private bool AuthenticateAdmin(string userName, string password)
        {
            List<Office> ofc = _myDmsEntity.Office.Where(r => r.Account_Email == userName).ToList();
            if (ofc.Count > 0)
            {
                if (ofc[0].Account_Password == password)
                {
                    Author = new Author(ofc[0].Office_Code, ofc[0].Account_Email, ofc[0].Office_Code, true, 0, 0);
                    return true;
                }
            }
            return false;
        }

        private bool AuthenticateUser(string userName, string password)
        {
            User user =
                _myDmsEntity.User.FirstOrDefault(r => (r.User_Name == userName) && (r.User_Password == password));
            if (user != null)
            {
                int dept_code = user.Department_Code;
                int role_code = _myDmsEntity.UserInRole.FirstOrDefault(r => r.User_Code == user.User_Code).Role_Code;
                Author = new Author(user.User_Code, user.User_Name,0, false, dept_code, role_code);
                return true;
            }
            return false;
        }


        protected void LoginOnlineUser(User user)
        {
            int dept_code = user.Department_Code;
            int role_code = _myDmsEntity.UserInRole.FirstOrDefault(r => r.User_Code == user.User_Code).Role_Code;
            Author = new Author(user.User_Code, user.User_Name,0, false, dept_code, role_code);
            Session["Author"] = Author;
        }

        protected void LoginOffice(Office office)
        {
            Author = new Author(office.Office_Code, office.Account_Email, office.Office_Code, true, 0, 0);
            Session["Author"] = Author;
        }

        #endregion

    }
}